﻿<%
var User = {
	HistorySize:		5,
	
	CurrentUser:		function(value){	

							if(value || value == ""){
								try{
									Session("CurrentUser") = value;
								}
								catch(e){
									return e + "";
								}
							}
							
							var currentUser = Session("CurrentUser");
							if(currentUser)
								currentUser += "";


							return (currentUser || undefined);

						},
						
	AllUsers:			{
							ToString:	function(){ return new String(Application("AllUsers") || ""); },
							ToList:		function(){return (User.AllUsers.ToString().length > 0 ? JSON.parse(User.AllUsers.ToString()) : []); },
							Add:		function(value, position){
											
											if(!value) return;
											
											value = new String(value);
											
											var list = User.AllUsers.ToList();
											if(position){
												list.splice(position, 0, value);
											}
											else{
												list.push(value);
											}
											
											Application("AllUsers") = JSON.stringify(list);
										},
							Remove:		function(value){
											var list = User.AllUsers.ToList();
											
											for(var x = 0, len = list.length; x < len; x++){
												if(value == list[x]){
													list.splice(x, 1);
												}
											}
											
											Application("AllUsers") = JSON.stringify(list);
										},
							Contains:	function(value){
											var list = User.AllUsers.ToList();
											for(var x = 0, len = list.length; x < len; x++){
												if(value == list[x]){
													return true;
												}
											}
											
											return false;
										}
						},
						
	UserHistory:		{
							ToString:	function(){ return new String(Request.Cookies("UserHistory") || ""); },
							ToList:		function(){ return (User.UserHistory.ToString().length > 0 ? JSON.parse(User.UserHistory.ToString()) : []); },
							Add:		function(value, position){
											if(!value) return;
											
											value = new String(value);
											
											position = position || 0;
											
											User.UserHistory.Remove(value);
											
											var list = User.UserHistory.ToList();

											list.splice(position, 0, value);
											list = list.slice(0, User.HistorySize);
											
											Response.Cookies("UserHistory") = JSON.stringify(list);
										},
										
							Remove:		function(value){
											var list = User.UserHistory.ToList();
																						
											for(var x = list.length - 1; x > -1; x--){
												var curList = list[x];

												if(value == curList){
													list.splice(x, 1);
												}
											};

											Response.Cookies("UserHistory") = JSON.stringify(list);
										},
										
							Contains:	function(value){
											var list = User.UserHistory.ToList();
											
											for(var x = 0, len = list.length; x < len; x++){
												if(value == list[x]){
												
													return true;
												}
											}
											
											return false;
										}
						},
	
	Debug:				function(outputType, label){
							switch(outputType){
								case "response":
								
									var user = User.CurrentUser();
									var allUserString = User.AllUsers.ToString();
									var allUsersList = User.AllUsers.ToList();
									var historyString = User.UserHistory.ToString();
									var historyList = User.UserHistory.ToList();
									
									Response.Write("<br>");
									Response.Write(label);
									Response.Write("CurrentUser: " + user);
									Response.Write("<br>AllUsers.String: " + allUserString);
									Response.Write("<br>AllUsers.List: " + allUsersList);
									Response.Write("<br>UserHistory.String: " + historyString);
									Response.Write("<br>UserHistory.List: " + historyList);
									Response.Write("<br>");
								break;
							}
						},
	
	ResetHistory:		function(){
							Response.Cookies("UserHistory") = "";
						}
	
	
	
}


%>